<!--
 * @Author: your name
 * @Date: 2021-08-20 09:57:55
 * @LastEditTime: 2021-08-29 19:22:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue电商管理平台\vue-e-commerce\src\components\Login.vue
-->

<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/avatar.jpg" alt="头像" />
      </div>
      <el-form
        ref="loginFormRef"
        :model="formData"
        :rules="verificationRules"
        label-width="70px"
        class="login_form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button @click="submitFormData" type="primary">登录</el-button>
          <el-button @click="resetLoginForm" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单数据
      formData: {
        username: "admin",
        password: "123456",
      },
      //表单验证规则
      verificationRules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            tigger: "blur",
          },
          {
            min: 3,
            max: 6,
            message: "长度在3到6个字符",
            tigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入登录密码",
            tigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在6到15个字符",
            tigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //提交数据
    submitFormData() {
      this.$refs.loginFormRef.validate(
        //elementui form组件的实例方法 表单预校验
        async (valid) => {
          if (!valid) return;
          const { data: res } = await this.$axios.post("login", this.formData);
          //处理返回的数据
          if (res.meta.status !== 200) return this.$message.error("登录失败");
          this.$message.success("登录成功");
          //把后端传过来的 tooken 保存在 sessionStorage 中
          window.sessionStorage.setItem("token", res.data.token);

          //跳转到后台主页
          this.$router.push("/home");
        }
      );
    },
    // 重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields(); //elementui form组件的实例方法
    },
  },
};
</script>

<style scoped>
.login_container {
  background-color: #7abdff;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_container .login_box {
  width: 600px;
  height: 400px;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0px 0px 10px #ddd;
}
.login_container .login_box .avatar_box {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  overflow: hidden;
  margin: -40px auto;
  box-shadow: 0 0 10px #ddd;
}
.login_container .login_box .avatar_box img {
  width: 100%;
  height: 100%;
}
.login_container .login_box .login_form {
  width: 480px;
  margin: 90px auto;
}

.btns {
  display: flex;
  /* 消除 <el-form label-width="70px"> 对按钮区域排版的影响 */
  margin-left: -70px;
  justify-content: center;
}
</style>>